﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 10. Write a program that extracts from a given text all 
			palindromes, e.g. "ABBA", "lamal", "exe". -->
	<style>
		textarea, button {
			display: block;
			width: 800px;
		}
	</style>
</head>
<body>
	<textarea id="input" placeholder="Enter text with palindromes for extraction..."></textarea>
	<button onclick="doChange()">Replace Anchors</button>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// Checks for palindrome word
		function isPalindrome(word) {
			var str = new String(word);
			var half = Math.floor(str.length / 2) - 1;
			var res = (half >= 0) ? true: false;

			for (var i = 0; i <= half; i++) {
				if (str[i] != str[str.length - 1 - i]) {
					res = false;
					break;
				}
			}

			return res;
		}

		function getPalindromes(text) {
			var words = text.split(/[\s\.,-?!)(]/);

			// Delete empty words
			for (var i = 0; i < words.length; i++) {
				if (words[i] == "") {
					words.splice(i, 1);
				}
			}

			var poly = new Array();
			// Delete non-palindromes
			for (var i = 0; i < words.length; i++) {
				if (isPalindrome(words[i])) {
					poly.push(words[i]);
				}
			}

			return poly;
		}

		// Button OnClick
		function doChange() {
			// Read User input
			var input = jsConsole.read("#input");

			// Output
			jsConsole.writeLine("You have entered:");
			jsConsole.writeLine("<blockquote>" + input + "</blockquote>");

			// Find them
			var result = getPalindromes(input);
			jsConsole.writeLine("All palindromes:");
			jsConsole.writeLine("<blockquote>" + result.join(", ") + "</blockquote>");
		}

	</script>
</body>
</html>
